# encoding: utf-8
"""
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    created by lane.chang on '11/05/2025'
    comment: API层统一依赖
"""
import asyncio
import functools
from pydantic import BaseModel

from project.model import ResponseModel


@ResponseModel
class ApiResponse(BaseModel):
    """
    """
    pass


def api_handler():
    """API统一封装
    :request: API请求体
    :return:
    """
    def handle_func(func):
        @functools.wraps(func)
        async def wrap_func(*args, **kwargs):
            try:
                if asyncio.iscoroutinefunction(func):
                    response = await func(*args, **kwargs)
                else:
                    response = func(*args, **kwargs)

            except Exception as ex:
                return ApiResponse.fail(code=-1, message=str(ex))

            return response

        return wrap_func

    return handle_func
